package cn.xaut.杂货;

/**
 * 665. 非递减数列
 */
public class demo665 {

    public boolean isSorted(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) 
                return false;
        }
        return true;
    }
    
    public boolean checkPossibility(int[] nums) {

        int len = nums.length;
        for (int i = 0; i < len - 1; i++) {
            int x = nums[i], y = nums[i + 1];
            if (x > y) {
                nums[i] = y;
                if (isSorted(nums))
                    return true;
                nums[i] = x;
                nums[i + 1] = x;
                return isSorted(nums);
            }
        }
        return true;
    }

    public static void main(String[] args) {

        System.out.println(new demo665().checkPossibility(new int[]{4, 2, 3}));     // true
        System.out.println(new demo665().checkPossibility(new int[]{4, 2, 1}));     // false
        System.out.println(new demo665().checkPossibility(new int[]{3, 4, 2, 3}));  // false
    }
}
